var form = document.getElementById('newTest');

form.onsubmit = function(e) {
	e.preventDefault();

	var questionId = getCookie("questionId");

	var test = {};
	questions = test.questions = [];
	var variants;
	console.log(form);
	var testId =  $("#newTest").attr('testId')

	for ( var i = 0, ii = form.length; i < ii; ++i) {
		var input = form[i];
		console.log(input);
		
		if (input.name) {
			var name = input.name;
			var value = input.value;
			
			
			if (name == "title") {
				test.title = value;
				test.id = testId;
			}
			if (name == "question") {
				variants = [];
				questions[questions.length] = {
					"testId" : "0",
					"questionId" : "0",
					"questionValue" : value
				};
				variants = questions[questions.length - 1].variants = [];

			}

			if (name == "variantTruth") {

				var truthBoolean = false;
				if (input.checked) {
					truthBoolean = true;
				}

				variants[variants.length] = {
					"testId" : testId,
					"questionId" : "0",
					"variantId" : "0",
					"variantValue" : "",
					"truth" : truthBoolean
				};

			}
			

			if (name == "variantValue") {
				
				variants[variants.length - 1].value = value;

			}

		}
	}

	test = JSON.stringify(test);

	console.log(test);

	$.ajax({
		type : 'POST',
		url : $("#newTest").attr('action'),
		data : test,
		success : function(msg) {
			console.log(msg);
			document.write(msg);
		}
	});

};

function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value)
			+ ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

function resetCookie() {
	var startValue = 0;
	setCookie("questionId", startValue);
	setCookie("answerId", startValue);
	setCookie("correctAnswerId", startValue);
}

function getCookie(c_name) {
	var c_value = document.cookie;
	var c_start = c_value.indexOf(" " + c_name + "=");
	if (c_start == -1) {
		c_start = c_value.indexOf(c_name + "=");
	}
	if (c_start == -1) {
		c_value = null;
	} else {
		c_start = c_value.indexOf("=", c_start) + 1;
		var c_end = c_value.indexOf(";", c_start);
		if (c_end == -1) {
			c_end = c_value.length;
		}
		c_value = unescape(c_value.substring(c_start, c_end));
	}
	return c_value;
}

function addQuestion() {

	var questionId = getCookie("questionId");

	var questionBody = document.createElement("question");
	document.getElementById("questions").appendChild(questionBody);

	var question = document.createElement("textarea");
	question.setAttribute("class",
			"input-xxlarge input-xxlarge-1 input-xxlarge-2");
	question.placeholder = "Question";
	question.name = "question";
	question.id = questionId;

	questionBody.appendChild(question);

	var separator = document.createElement("br");
	questionBody.appendChild(separator);

	var answers = document.createElement("div");
	answers.id = "answers";

	for ( var i = 0; i < 5; i++) {

		var answerBody = document.createElement("answer");

		var answer = document.createElement("textarea");
		answer.setAttribute("class",
				"input-xxlarge input-xxlarge-2 input-xxlarge-3");
		answer.placeholder = "Answer";

		var answerTruthBody = document.createElement("label");
		answerTruthBody.setAttribute("class", "checkbox pull-left");

		var answerTruth = document.createElement("input");
		answerTruth.type = "checkbox";
		answerTruth.name = "variantTruth";

		answerTruthBody.appendChild(answerTruth);

		answerBody.appendChild(answerTruthBody);

		var answer = document.createElement("textarea");
		answer.setAttribute("class",
				"input-xxlarge input-xxlarge-2 input-xxlarge-3");
		answer.placeholder = "Answer";
		answer.name = "variantValue";

		answerBody.appendChild(answer);

		answerBody.appendChild(separator);

		answers.appendChild(answerBody);

	}

	questionBody.appendChild(answers);
	questionBody.appendChild(separator);
	questionId = parseInt(questionId) + 1;
	setCookie("questionId", questionId);

}